<template lang="pug">
  .clearfix
    Modal(
      width="400"
      v-model="isShowChooseCompanyModal"
      title="选择公司"
      class-name="vertical-center-modal"
    )
      .search-form.mb-15.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :label-width="110")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.value"
                  :key="index"
                ) {{ c_item.label }}
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSubmit") 确定
</template>

<script>
import { Common } from '@/utils/common.js'

export default {
  name: '',

  props: {
  },

  data () {
    return {
      isShowChooseCompanyModal: false,
      formData: {
        companyName: ''
      },
      formItem: [
        { type: 'select', label: '公司名称：', placeholder: '公司名称', prop: 'companyName', model: 'companyName', options: 'customer' },
      ],
      arrListDown: {
        customer: []
      },
    }
  },

  watch: {
  },

  mounted () {
    this.companys()
  },

  methods: {
    cancel () {
      this.isShowChooseCompanyModal = false
    },
  
    show () {
      this.isShowChooseCompanyModal = true
      this.formData.companyName = ''

      if (this.arrListDown.customer.length == 1) {
        console.log('this.arrListDown.customer: ', this.arrListDown.customer)
        this.formData.companyName = this.arrListDown.customer[0].value
        this.handleSubmit()
      }
    },

    // 获取公司
    async companys () {
      var params = {
        PageIndex: 1,
        PageSize: 10000,
        Args: ''
      }
      Common.companys(params, this.arrListDown, 'customer')
    },

    handleSubmit () {
      if (this.formData.companyName) {
        var companyObj = this.arrListDown.customer.find(item => {
          return item.value === this.formData.companyName
        })
        this.$emit('goEmployeeCreate', companyObj)
        this.cancel()
      } else {
        this.$Message.error({
          content: '请选择公司',
          duration: 10,
          closable: true
        })
      }
    }
  }
}
</script>

<style lang="less" scope>
.ApplyInvoice{

}
</style>
